# -*- coding: utf-8 -*-
# @Time: 07/12/2024 16:21
# @Author: Axel
# @File: exception.py
# @Comment: 异常处理

from rest_framework.views import exception_handler
from ..base.constant import CODE_NOT_FOUND_ERROR, MSG_NOT_FOUND_ERROR, CODE_AUTH_ERROR, CODE_SERVER_ERROR, \
    MSG_SERVER_ERROR, MSG_REJECT_ERROR, CODE_REJECT_ERROR, CODE_METHOD_ERROR, MSG_METHOD_ERROR, MSG_PARAMETER_ERROR, \
    CODE_PARAMETER_ERROR


def custom_exception_handler(exc, context):
    response = exception_handler(exc, context)

    if response is not None:
        response.data.clear()
        response.data['data'] = {}

        if response.status_code == 404:
            try:
                response.data['message'] = MSG_NOT_FOUND_ERROR
                response.data['code'] = CODE_NOT_FOUND_ERROR
            except KeyError:
                response.data['message'] = MSG_NOT_FOUND_ERROR

        if response.status_code == 400:
            response.data['message'] = MSG_PARAMETER_ERROR
            response.data['code'] = CODE_PARAMETER_ERROR

        elif response.status_code == 401:
            response.data['message'] = exc.detail
            response.data['code'] = CODE_AUTH_ERROR

        elif response.status_code >= 500:
            response.data['message'] = MSG_SERVER_ERROR
            response.data['code'] = CODE_SERVER_ERROR

        elif response.status_code == 403:
            response.data['message'] = MSG_REJECT_ERROR
            response.data['code'] = CODE_REJECT_ERROR

        elif response.status_code == 405:
            response.data['message'] = MSG_METHOD_ERROR
            response.data['code'] = CODE_METHOD_ERROR

        return response
